package com.stud.view;

import com.stud.service.ServiceWrapper;
import com.stud.util.JOptionPaneWrapper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ClientView {

    private final Widgets widgets = new Widgets();
    private final ClientController controller;

    public ClientView(ServiceWrapper service) {
        this.controller = new ClientController(service, this, new JOptionPaneWrapper());

        initWidgets();
        initLayout();
    }

    public void display() {
        widgets.clientFrame.setVisible(true);
    }

    public JFrame getClientFrame() {
        return widgets.clientFrame;
    }

    public String getLastName() {
        return widgets.lastNameField.getText();
    }

    public String getFirstName() {
        return widgets.firstNameField.getText();
    }

    private void initWidgets() {
        initClientFrame();
        initFirstName();
        initLastName();
        initSaveButton();
        initDeleteButton();
    }

    private void initClientFrame() {
        widgets.clientFrame = new JFrame("Client");
    }

    private void initFirstName() {
        widgets.firstNameLabel = new JLabel("First Name");
        widgets.firstNameField = new JTextField();
    }

    private void initLastName() {
        widgets.lastNameLabel = new JLabel("Last Name");
        widgets.lastNameField = new JTextField();
    }

    private void initSaveButton() {
        widgets.saveButton = new JButton("Save");
        widgets.saveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                controller.saveClient();
            }
        });
    }

    private void initDeleteButton() {
        widgets.deleteButton = new JButton("Delete");
        widgets.deleteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                controller.deleteClient();
            }
        });
    }

    private void initLayout() {
        widgets.clientFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        widgets.clientFrame.setSize(300, 200);
        widgets.clientFrame.setLocation(800, 400);
        widgets.clientFrame.setLayout(new GridLayout(0, 2));

        widgets.clientFrame.add(widgets.firstNameLabel);
        widgets.clientFrame.add(widgets.firstNameField);
        widgets.clientFrame.add(widgets.lastNameLabel);
        widgets.clientFrame.add(widgets.lastNameField);
        widgets.clientFrame.add(widgets.saveButton);
        widgets.clientFrame.add(widgets.deleteButton);
    }

    private static class Widgets {

        private JFrame clientFrame;
        private JLabel firstNameLabel;
        private JTextField firstNameField;
        private JLabel lastNameLabel;
        private JTextField lastNameField;
        private JButton saveButton;
        private JButton deleteButton;
    }
}
